package cn.hdustea.shopNavigation.ui.view.main

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View

import androidx.recyclerview.widget.LinearLayoutManager
import cn.hdustea.shopNavigation.R
import cn.hdustea.shopNavigation.entity.Beauty
import cn.hdustea.shopNavigation.entity.Mall
import cn.hdustea.shopNavigation.ui.contract.MallSearchContract
import cn.hdustea.shopNavigation.ui.presenter.MallSearchPresenter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hdustea.base.base.BaseActivity
import com.hdustea.base.widget.CustomLoadMoreView
import kotlinx.android.synthetic.main.activity_search_result.*
import kotlinx.android.synthetic.main.item_list_mall.view.rt_bar
import kotlinx.android.synthetic.main.item_list_mall.view.*


class MallSearchActivity : BaseActivity<MallSearchContract.Presenter>(), MallSearchContract.View {

    private var searchAdapter: SearchAdapter? = null

    private lateinit var keyword:String



    companion object {

        fun startActivity(context: Context) {
            context.startActivity(Intent(context, MallSearchActivity::class.java))
        }

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if(intent.extras != null) {
            keyword = intent.getStringExtra("extra_data").toString()
            Log.d("data", keyword)
        }
    }

    override fun initView() {
        val layoutManager = LinearLayoutManager(this)
        search_result_view.layoutManager = layoutManager
        search_result_view.isNestedScrollingEnabled = true
        showLoading()
    }

    override fun initData() {
        mPresenter?.getSearchList(keyword, false)
    }

    private fun initRecyclerView() {
        searchAdapter!!.animationEnable = true
        searchAdapter!!.loadMoreModule.loadMoreView = CustomLoadMoreView()
//        searchAdapter!!.loadMoreModule.setOnLoadMoreListener {
//            mPresenter?.getSearchList(mCurrentPage + 1, true)
//        }
        searchAdapter!!.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        searchAdapter!!.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
    }

    override fun getLayoutRes(): Int = R.layout.activity_search_result

    override fun getPresenter(): MallSearchContract.Presenter = MallSearchPresenter(this)

    override fun setSearchList(mallList: MutableList<Mall>, isRefresh: Boolean) {
        if (!isRefresh) {
            searchAdapter = SearchAdapter(R.layout.item_list_mall)
            initRecyclerView()
            search_result_view.adapter = searchAdapter
            searchAdapter!!.setList(mallList)
        }
//        else {
//            if (beautyList.isNotEmpty()) {
//                mCurrentPage++
//                searchAdapter!!.addData(beautyList)
//                searchAdapter!!.loadMoreModule.loadMoreComplete()
//            } else {
//                searchAdapter!!.loadMoreModule.loadMoreEnd()
//            }
//        }
    }



    override fun onError() {
        searchAdapter!!.loadMoreModule.isEnableLoadMore = true
        searchAdapter!!.loadMoreModule.loadMoreFail()
    }

    inner class SearchAdapter(layoutRes: Int) :
            BaseQuickAdapter<Mall, BaseViewHolder>(layoutRes), LoadMoreModule {

        private val OPTIONS: RequestOptions = RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .dontAnimate()

        override fun convert(holder: BaseViewHolder, item: Mall) {
            val rootView = holder.itemView
            rootView.tv_mall_room_name.text = item.name
            rootView.rt_bar.rating = item.stars
            if (item.consume_per == 0) {
                rootView.tv_mall_room_name.visibility = View.GONE
            } else {
                rootView.tv_mall_average_price.text =
                        String.format(context.resources.getString(R.string.food_price), item.consume_per)
                rootView.tv_mall_average_price.visibility = View.VISIBLE
            }
            rootView.tv_mall_type.text = item.tag
            Glide.with(context).load(item.picurl).apply(OPTIONS).into(rootView.iv_mall_1)

//            if (item.picurls.size >= 1) {
//                Glide.with(context).load(item.picurls[0]).apply(OPTIONS).into(rootView.iv_beauty_1)
//                if (item.picurls.size >= 2) {
//                    Glide.with(context).load(item.picurls[1]).apply(OPTIONS).into(rootView.iv_beauty_2)
//                    if (item.picurls.size >= 3) {
//                        Glide.with(context).load(item.picurls[2]).apply(OPTIONS).into(rootView.iv_beauty_3)
//                    }
//                }
//            }
//            rootView.to_navigation.setOnClickListener {
//                val intent  = Intent(pContext, MainActivity::class.java)
//                intent.putExtra("searchText", rootView.tv_beauty_room_name.text.toString())
//                pContext.startActivity(intent)
//            }

//            Glide.with(context).load(R.drawable.ic_navigation).apply(OPTIONS).into(rootView.to_navigation)


        }

    }

}
